package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type OperateMethodEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名

	OperateName string // 操作命令
	OperateMode string // 操作模式
	DataType    string // 返回的数据类型
	EngineType  string // 引擎类型
	ServiceType string // 业务类型
	Timeout     int    // 通信超时
	Polling     bool   // 该操作是否需要被轮询调度

	EngineParam map[string]interface{} // 配置参数
}

type operateMethodPo struct {
	Id           int64                  `json:"id"`           // 对象id
	CreateTime   int64                  `json:"createTime"`   // 创建时间
	UpdateTime   int64                  `json:"updateTime"`   // 更新时间
	Manufacturer string                 `json:"manufacturer"` // 设备厂商名称
	DeviceType   string                 `json:"deviceType"`   // 设备类型名
	OperateName  string                 `json:"operateName"`  // 操作命令
	OperateMode  string                 `json:"operateMode"`  // 操作模式
	DataType     string                 `json:"dataType"`     // 返回的数据类型
	EngineType   string                 `json:"engineType"`   // 引擎类型
	ServiceType  string                 `json:"serviceType"`  // 业务类型
	Timeout      int                    `json:"timeout"`      // 通信超时
	Polling      bool                   `json:"polling"`      // 该操作是否需要被轮询调度
	EngineParam  map[string]interface{} `json:"engineParam"`  // 配置参数
}

func (e *OperateMethodEntity) Type() string {
	return "OperateMethodEntity"
}

func (e *OperateMethodEntity) TableName() string {
	return ""
}

func (e *OperateMethodEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *OperateMethodEntity) Instance() *OperateMethodEntity {
	return &OperateMethodEntity{
		BaseEntity:   new(commEntity.BaseEntity),
		Manufacturer: "",
		DeviceType:   "",
		OperateName:  "",
		OperateMode:  "",
		DataType:     "",
		EngineType:   "",
		ServiceType:  "",
		Timeout:      0,
		Polling:      false,
		EngineParam:  make(map[string]interface{}),
	}
}

func (e *OperateMethodEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *OperateMethodEntity) NewPo() interface{} {
	return &operateMethodPo{
		EngineParam: make(map[string]interface{}),
	}
}

func (e *OperateMethodEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *OperateMethodEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.Manufacturer)
	p = append(p, e.DeviceType)
	p = append(p, e.OperateName)
	return p
}

func (e *OperateMethodEntity) BuildByJson(jsn *string) error {
	var po operateMethodPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *OperateMethodEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *OperateMethodEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*operateMethodPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.EngineParam = po.EngineParam
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.OperateName = po.OperateName
	ptr.OperateMode = po.OperateMode
	ptr.DataType = po.DataType
	ptr.EngineType = po.EngineType
	ptr.ServiceType = po.ServiceType
	ptr.Timeout = po.Timeout
	ptr.Polling = po.Polling
}

func (e *OperateMethodEntity) BuildToPo() interface{} {
	ptr := e
	po := operateMethodPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.EngineParam = ptr.EngineParam
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.OperateName = ptr.OperateName
	po.OperateMode = ptr.OperateMode
	po.DataType = ptr.DataType
	po.EngineType = ptr.EngineType
	po.ServiceType = ptr.ServiceType
	po.Timeout = ptr.Timeout
	po.Polling = ptr.Polling

	return &po
}
